import {Injectable} from "@angular/core";
import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import 'rxjs/add/observable/throw';
import {Observable} from "rxjs/Observable";

@Injectable()
export class ExceptionHandler {

  constructor() {
  }

  public handleError(error: Response | any): Observable<any> {
    let msg = error.json().message ? error.json().message : '请求失败';
    if (error.status == 0) {
      msg = '网络连接异常';
    }
    if (error.status == 400) {
      msg = '无效请求,请检测参数是否正确';

    }
    if (error.status == 401) {
      msg = '未授权,请登录';
    }
    if (error.status == 404) {
      msg = '请求资源不存在';
    }
    console.error(error);
    return Observable.throw({status: error.json().status, code: error.json().code, message: msg})
  }
}
